package com.stylefeng.guns.generator.engine.util;

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;

import java.awt.geom.Point2D;
import java.io.File;

public class SplitPDF {
    public static void main(String[] args)
    {
        File file = new File("C:\\Users\\Administrator\\Desktop\\jar\\test.pdf");
        //加载PDF文档
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\jar\\test.pdf");

        //新建一个PDF文档
        PdfDocument newDoc1 = new PdfDocument();

        PdfPageBase page;

        //将原PDF文档的第1、2页添加至新建的PDF
        for(int i = 0;i<2;i++)
        {
            page = newDoc1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }

        //保存文档
        newDoc1.saveToFile("C:\\Users\\Administrator\\Desktop\\jar\\Doc1.pdf");

        //新建另一个PDF文档
        PdfDocument newDoc2 = new PdfDocument();

        //将原PDF文档的第3至5页添加至新建的PDF
        for(int i = 2;i<5;i++)
        {
            page = newDoc2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }

        //保存文档
        newDoc2.saveToFile("C:\\Users\\Administrator\\Desktop\\jar\\Doc2.pdf");
    }
}